/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.cloudlayouter;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;

import twaver.network.TNetwork;
import twaver.network.ui.ResizableNodeUI;

public class DemoImageNodeUI extends ResizableNodeUI{
	private DemoImageNode demoNode;
	
	public DemoImageNodeUI(TNetwork network, DemoImageNode node) {
		super(network, node);
		this.demoNode=node;
	}
	public void paintBody(Graphics2D g2) {
		Rectangle bounds = demoNode.getBounds();
		g2.setColor(new Color(180,180,180,200));
		g2.fillRect(bounds.x, bounds.y, bounds.width,bounds.height);
		g2.setColor(Color.WHITE);
		int w = (int)(bounds.width*0.9);
		int h = (int)(bounds.height*0.9);
		g2.fillRect(bounds.x+(bounds.width-w)/2, bounds.y+(bounds.height-h)/2, w, h);
		if(!demoNode.isDemoLoaded()){
			drawUnloadedDemoNode(demoNode.getDemoNode().getName(), bounds.x, bounds.y, bounds.width, bounds.height, g2, bounds.width/13);
		}
		if(demoNode.getDemoImage()!=null){
			g2.drawImage(demoNode.getDemoImage(), bounds.x+(bounds.width-w)/2, bounds.y+(bounds.height-h)/2, w, h, null);
		}
	}
	public boolean contains(int x, int y) {
		return demoNode.getBounds().contains(x, y);
	}

	private static void drawUnloadedDemoNode(String demoName, int x, int y, int width, int height, Graphics2D g2, int fontSize) {
		g2.setColor(Color.BLACK);
		if(fontSize<1){
			fontSize=1;
		}
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setFont(new Font("Tahoma",Font.PLAIN,fontSize));
		FontRenderContext frc = g2.getFontRenderContext();
		Rectangle2D bounds = g2.getFont().getStringBounds(demoName, frc);
		g2.drawString(demoName, (float) (x+(width-bounds.getWidth())/2), (float)(y+height/2)-10);
		bounds = g2.getFont().getStringBounds("double click and load", frc);
		g2.drawString("double click and load", (float)(x+(width-bounds.getWidth())/2), (float)(y+height/2+bounds.getHeight()));
	}
}